<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/14
 * Time: 17:39
 */
namespace App\Http\Controllers\Api\V2_0_0;

use App\Http\Requests\Api\V2_0_0\RepaymentPlanRequest;
use App\Services\RepaymentPlanService;
use Illuminate\Http\Request;

class RepaymentPlanController extends BaseController
{
    protected $repaymentPlanService;

    public function __construct(RepaymentPlanService $repaymentPlanService)
    {
        parent::__construct();
        $this->repaymentPlanService = $repaymentPlanService;
    }

    /**
     * 提交还款计划
     */
    public function confirmRepaymentPlan(RepaymentPlanRequest $repaymentPlanRequest)
    {
        if (!empty($repaymentPlanRequest->consume_address)) {
            $consumeAddress = json_decode($repaymentPlanRequest->consume_address, true);
            if (empty($consumeAddress['province']) || empty($consumeAddress['city'])
                || empty($consumeAddress['area'])) {
                return $this->responseJson(10524);//消费地址格式不正确
            }
        } else {
            $consumeAddress = [
                'province' => '广东省',
                'city' => '广州市',
                'area' => '天河区',
            ];
        }
        return $this->repaymentPlanService->makePlan($this->user, $repaymentPlanRequest->id,
            $repaymentPlanRequest->repayment_plan_data, $repaymentPlanRequest->expire_date, $repaymentPlanRequest->cvn2,
            $repaymentPlanRequest->consume_credit_card_id, $consumeAddress);
    }

    /**
     * 还款计划详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function repaymentPlanDetail(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->repaymentPlanDetail($request->plan_id, $this->user['user_id']);
    }

    /**
     * 更新计划状态为停止
     */
    public function updatePlanStatusToStop(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->updatePlanStatusToStop($request->plan_id, $this->user['user_id']);
    }

    /**
     * 计划继续执行
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updatePlanStatusToContinue(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->updatePlanStatusToContinue($request->plan_id, $this->user, $request->expire_date, $request->cvn2);
    }

    /**
     * 删除计划
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->delete($request->plan_id, $this->user['user_id']);
    }
}